package com.q.tqb.fragment;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.mine.MineRedEnvelopeActivity;
import com.q.tqb.adapter.BaseFragmentPagerAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.RedStatisticsInfo;
import com.q.tqb.util.DensityUtil;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocietyFragment extends Fragment {


    @BindView(R.id.tab_grab)
    TabLayout mTabGrab;
    @BindView(R.id.vp_grab)
    ViewPager mVpGrab;

    Unbinder unbinder;


    private BaseFragmentPagerAdapter mFragmentPagerAdapter;

    public SocietyFragment() {
    }

    public static SocietyFragment newInstance() {
        SocietyFragment fragment = new SocietyFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_society, container, false);
        unbinder = ButterKnife.bind(this, view);


        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            RankFragment grabDetailFragment = RankFragment.newInstance(i);
            fragments.add(grabDetailFragment);
        }

        String[] tabStrings = getActivity().getResources().getStringArray(R.array.RankRedEnvelope);
        mFragmentPagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), getActivity(), fragments, tabStrings);

        mVpGrab.setAdapter(mFragmentPagerAdapter);
        mTabGrab.setupWithViewPager(mVpGrab);
        mTabGrab.setTabMode(TabLayout.MODE_FIXED);

        LinearLayout linearLayout = (LinearLayout) mTabGrab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider));
        linearLayout.setDividerPadding(DensityUtil.dip2px(getActivity(), 10.0f)); //设置分割线间隔

        mTabGrab.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabGrab, 50, 50);
            }
        });
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }

}
